package com.uniview.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.uniview.entity.vo.Query;
import com.uniview.entity.vo.QueryBuilder;

public class QueryBuilderUtils {

	/**
	 * 查询参数映射
	 */
	private Map<String, Object> paramsMap = new HashMap<String, Object>();

	/**
	 * 组装HQL查询语句
	 * 
	 * @param qBuilder
	 * @return
	 */
	public String builderHQL(QueryBuilder queryBuilder) {
		if (queryBuilder == null || StringUtils.isBlank(queryBuilder.getEntityName()))
			return null;
		// 查目标对象
		StringBuffer hqlBuf = new StringBuffer();
		if (queryBuilder.getEntityName().toLowerCase().indexOf("from") < 0)
			hqlBuf.append("from "); // 若缺少from, 则追加上去
		hqlBuf.append(queryBuilder.getEntityName());
		if (queryBuilder.getQueryList() != null) {
			int idex = 0;
			for (Query query : queryBuilder.getQueryList()) {
				if (query == null)
					continue;
				// 逐条组装查询条件
				String queryCondition = builderQuery(query);
				if (StringUtils.isNotBlank(queryCondition)) {
					// 衔接条件
					if (idex++ == 0 && hqlBuf.toString().toLowerCase().indexOf("where") < 0
							&& hqlBuf.toString().toLowerCase().indexOf("join") < 0) {
						// 若缺少 where, 则追加上去
						hqlBuf.append(" where ").append(queryCondition);
					} else {
						hqlBuf.append(query.getAndOr()).append(queryCondition);
					}
				}
			}
		}
		if (queryBuilder.getOrderCols() != null) {
			// 累加排序字段
			String orderCols = "";
			for (String orderCol : queryBuilder.getOrderCols()) {
				if (StringUtils.isNotBlank(orderCol)) {
					orderCols += orderCol + ",";
				}
			}
			if (orderCols.endsWith(",")) {// 剔除末尾的","
				orderCols = orderCols.substring(0, orderCols.length() - 1);
			}
			if (StringUtils.isNotBlank(orderCols)) {
				// 拼装排序字段及排序方式
				hqlBuf.append(" order by ").append(orderCols).append(queryBuilder.getOrderMode().toString());
			}
		}
		// 返回拼装后的HQL语句
		return hqlBuf.toString();
	}

	/**
	 * 组装单条的查询条件
	 * 
	 * @param query
	 * @return
	 */
	private String builderQuery(Query query) {
		if (query != null && StringUtils.isNotBlank(query.getColName()) && query.getMatchMode() != null) {
			String colName = query.getColName();
			switch (query.getMatchMode()) {
			case IS_NULL: // 为空
				return colName + query.getMatchMode().toString();
			case IS_NOT_NULL: // 非空
				return colName + query.getMatchMode().toString();
			case BETWEEN: // between ... and ...
				Object[] colVals = query.getColVal();
				if (colVals != null && colVals.length >= 2) {
					String start = "startVal";
					String end = "endVal";
					paramsMap.put(start, colVals[0]);
					paramsMap.put(end, colVals[1]);
					return colName + " between :" + start + " and :" + end;
				}
				break;
			case IN: // in 子句
				Object[] inVals = query.getColVal();
				if (inVals != null && inVals.length > 0) {
					paramsMap.put(colName, query.getColVal());
					return colName + query.getMatchMode().toString() + ":" + colName;
				}
				break;
			case NOT_IN: // not in 子句
				Object[] notInVals = query.getColVal();
				if (notInVals != null && notInVals.length > 0) {
					String valArr = "";
					for (Object boj : notInVals) {
						if (boj != null) {
							valArr += boj + ",";
						}
					}
					if (valArr.endsWith(",")) {
						valArr = valArr.substring(0, valArr.length() - 1);
					}
					if (valArr.length() > 0) {
						paramsMap.put(colName, "(" + valArr + ")");
						return colName + query.getMatchMode().toString() + ":" + colName;
					}
				}
				break;
			case LIKE: // like 子句
				Object[] likeVals = query.getColVal();
				if (likeVals != null && likeVals.length > 0) {
					paramsMap.put(colName, "%" + likeVals[0] + "%");
					return colName + query.getMatchMode().toString() + ":" + colName;
				}
				break;
			case LLIKE: // like 子句
				Object[] likeVal2 = query.getColVal();
				if (likeVal2 != null && likeVal2.length > 0) {
					paramsMap.put(colName, "%" + likeVal2[0]);
					return colName + query.getMatchMode().toString() + ":" + colName;
				}
				break;
			case RLIKE: // like 子句
				Object[] likeVal3 = query.getColVal();
				if (likeVal3 != null && likeVal3.length > 0) {
					paramsMap.put(colName, likeVal3[0] + "%");
					return colName + query.getMatchMode().toString() + ":" + colName;
				}
				break;
			default: // 其它
				Object[] vals = query.getColVal();
				if (vals != null && vals.length > 0) {
					paramsMap.put(colName, vals[0]);
					return colName + query.getMatchMode().toString() + ":" + colName;
				}
				break;
			}
		}
		return null;
	}

	public Map<String, Object> getParamsMap() {
		return paramsMap;
	}

	public void setParamsMap(Map<String, Object> paramsMap) {
		this.paramsMap = paramsMap;
	}

}
